package leetcode.weekly.week312;

//Solution2Test
public class Solution2 {

	public int longestSubarray(int[] nums) {
		int max = 0;
		for (int num : nums) {
			max = Math.max(max, num);
		}
		int l = 0, r = 0, ans = 0;
		while (r < nums.length) {
			while (r < nums.length && !this.f(max, nums[r])) {
				r++;
			}
			l = r;
			while (r < nums.length && this.f(max, nums[r])) {
				r++;
			}
			ans = Math.max(ans, r - l);
		}
		return ans;
	}

	private boolean f(int max, int num) {
		while (max > 0) {
			if ((max & 1) == 1) {
				if ((num & 1) == 0) {
					return false;
				}
			}
			max >>= 1;
			num >>= 1;
		}
		return true;
	}

}
